package com.twu.biblioteca;

import com.twu.model.Book;
import com.twu.model.Customer;
import com.twu.model.Library;
import com.twu.model.Movie;

import java.util.Random;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: FSW
 * Date: 31/05/13
 * Time: 下午 07:49
 * To change this template use File | Settings | File Templates.
 */
public class Main {
    private static int choice;
    private static Scanner sc;

    public static void main(String[] args){
        sc = new Scanner(System.in);
        initLibrary();
        menuDisplay();

        while(choice != 9){
            if(Constants.user == null){
                switch (choice){
                    case 1:
                        printBooks();
                        getInput();
                        break;
                    case 2:
                        printMovies();
                        getInput();
                        break;
                    case 3:
                        login();
                        break;
                    default:
                        System.out.println("Please select a valid option.");
                        getInput();
                        break;
                }
            }
            else{
                switch (choice){
                    case 1:
                        printBooks();
                        getInput();
                        break;
                    case 2:
                        printMovies();
                        getInput();
                        break;
                    case 3:
                        reserve();
                        getInput();
                        break;
                    case 4:
                        Constants.user.printProfile();
                        getInput();
                        break;
                    case 5:
                        logout();
                        break;
                    default:
                        System.out.println("Please select a valid option.");
                        getInput();
                        break;
                }
            }
        }
    }

    private static void printBooks() {
        System.out.println("Title   Author    ISBN    Location");
        Constants.library.printBooks();
    }

    private static void printMovies() {
        System.out.println("Movie   Year    Director    Rating");
        Constants.library.printMovies();
    }

    private static void reserve() {
        System.out.println("Please input the ISBN:");
        String isbn = sc.next();
        if(Constants.library.reserve(Constants.library.getBookByIsbn(isbn), Constants.user))
            System.out.println("Thank You! Enjoy the book.");
        else
            System.out.println("Sorry we don't have that book yet.");
    }

    private static void logout() {
        Constants.user = null;
        System.out.println("Successfully logged out");
        menuDisplay();
    }

    private static void getInput() {
        System.out.println("Input your option");
        choice = sc.nextInt();
    }

    private static void login() {
        String username;
        String password;
        System.out.println("Input username:");
        username = sc.next();
        System.out.println("Input password:");
        password = sc.next();
        Constants.user = Login.login(Constants.library, username, password);
        if(Constants.user != null)
            System.out.println("Logged in successfully");
        else
            System.out.println("Invalid username/password");
        menuDisplay();
    }

    private static void menuDisplay() {
        System.out.println("-----------Biblioteca----------");
        System.out.println("1. List all books");
        System.out.println("2. List all movies");
        if(Constants.user == null) {
            System.out.println("3. Login");

        }
        else{
            System.out.println("3. Reserve book");
            System.out.println("4. Check profile");
            System.out.println("5. Log out");
        }
        System.out.println("9. Leave system");
        getInput();
    }

    private static void initLibrary() {
        Constants.library = new Library();
        //add books
        FileParser fileParser = new FileParser("TWU_Biblioteca-master/inputs/books.txt");
        for(String[] bookInfo:fileParser.getData()){
            Book book = new Book(bookInfo[1], bookInfo[2], bookInfo[0], bookInfo[3]);
            Constants.library.addBook(book);
        }
        //add movies
        fileParser = new FileParser("TWU_Biblioteca-master/inputs/movies.txt");
        for(String[] movieInfo:fileParser.getData()){
            Book movie = null;
            if(movieInfo.length == 5) {
                movie = new Movie(movieInfo[0], movieInfo[2], movieInfo[3], movieInfo[4], Integer.parseInt(movieInfo[1]));
            }
            else if(movieInfo.length == 6){
                System.out.println(movieInfo[1]);
                movie = new Movie(movieInfo[0], movieInfo[2], movieInfo[3], movieInfo[4], movieInfo[5], Integer.parseInt(movieInfo[1]));
            }
            Constants.library.addMovie((Movie) movie);
        }
        //add customers
        fileParser = new FileParser("TWU_Biblioteca-master/inputs/customers.txt");
        for(String[] customerInfo:fileParser.getData()){
            Customer customer = new Customer(customerInfo[1], customerInfo[2], customerInfo[3],
                    Long.parseLong(customerInfo[4]));
            Constants.library.addCustomer(customer);

        }
    }


}
